#ifndef RS485_H
#define RS485_H 

//#include "main.h"
#include "string.h"
#include <stdint.h>
#include <stdio.h>


typedef struct {		     
        uint8_t   Uart_txdr[8];  //串口发送数据
        uint8_t   Uart_rxdr[8];  //串口接收数据
	      uint16_t  fact_BUS_Curr; // 实际母线电流
	      uint16_t  fact_BUS_Voil; // 实际母线电压
		    uint16_t  iq_test;       // q轴测试电流
	      uint16_t  id_test;       // d轴测试电流
	      uint16_t  uq_test;       // q轴测试电压
	      uint16_t  ud_test;       // d轴测试电压
	      int16_t   IqRef_test;    // d轴给定参数电流
	      uint16_t  Speed_target;  // 目标转速
	      uint16_t  Speed_fact;    // 实际转速
        uint8_t   Rece_flag;     // 接收标志  
	      uint8_t   Rxdr_byte;     // 接收字节数
        uint8_t   chaoshi_jishu;  //超时判断计数
        uint8_t   chaoshi_pand;   //超时判断时间
        uint8_t   chaoshi_pandold;//历史超时判断时间
        uint8_t   chaoshi_shu;    //超时判断数
	   }Test;
#define  Test_DEFAULTS  {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}  // 初始化参数

typedef enum
{
	NONE_ERR,
	LV_ERR,
	OV_ERR,
	OC_ERR,
	DRV_ERR
} enMcErr;

typedef enum
{
	MC_RDY,
	MC_START,
	MC_RUN,
	MC_STOP,
	MC_ERR
} enMcState;

typedef enum
{
	START_CMD = 1,
	STOP_CMD = 0
} enMcCMD;

typedef struct
{
	enMcCMD cmd;			// uint16.
	enMcState mc_state;
	enMcErr	mc_err;

	int32_t refIq;			// sfix16_en9.
	int32_t refRPM;			// sfix16_en2.

	int16_t isens_a;		// sfix16_en8.
	int16_t isens_b;		// sfix16_en8.
	int16_t isens_c;		// sfix16_en8.
	uint32_t vbus;			// ufix16_en9.
	uint16_t encoder_theta;	// ufix16_13.
	int16_t encoder_count;
	uint16_t encoder_dir;
	
	uint32_t run_realtime;
	uint8_t discharge_on;
	uint8_t err_code;
	uint8_t err_count;
	uint32_t err_state_vbus;
} stMcInfo;
extern stMcInfo mc_info;

extern volatile int16_t comm[6];;

int myprintf(const char *format,...);
void RS485_SetToSendMode(void);
void RS485_SendData(uint8_t *data, uint16_t size);
void RS485_StartReceive(void);
void SendUint32(uint32_t value);
void SendUint32AsDecimalString(uint32_t value);

unsigned short usMBCRC16( unsigned char * pucFrame, unsigned short usLen );
void UploadData(stMcInfo *mc_info);
#endif

